var app = getApp(),
  that;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showView: true,
    vipLevelInfo: {},
    bizUserInfo: {},
    userInfo: {},
    addInfo: {},
    amt: 0.0,
    couponAmt: 0,
    total: '',
    qlq: !0,
    listarr: ["代金券", "折扣券"],
    activeIndex: 0,
    djq: null,
    zkq: null,
    activeradio: '',
    yhqnum: '',
    yhqfull: '',
    yhqname: '',
    yhqkdje: '',
    kdje: '',
    totalprice: '',
    couponId: '',
    showModal: !1,
    kqyue: !0,
    kong: !0,
    inputValue: ''
  },
  onChangeShowState: function() {
    var that = this;
    that.setData({
      showView: (!that.data.showView)
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    that = this
    console.log(options);
    showView: (options.showView == "true" ? true : false)
    that.setData({
      vipLevelInfo: wx.getStorageSync('vipLevelInfo'),
      addInfo: app.globalData.shopData
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.lqyhq()
    // 获取余额
    var bizUserInfo = wx.getStorageSync('bizUserInfo')
    console.log(bizUserInfo)
    this.setData({
      balance: bizUserInfo.balance,
      bizUserInfo: bizUserInfo
    })
  },
  getMoney(e) {
    if (wx.getStorageSync('vipLevelInfo') == null || wx.getStorageSync('vipLevelInfo') == '') {
      console.log(e.detail.value)
      that.setData({
        total: e.detail.value,
        amt: e.detail.value,
      })
    } else {
      console.log(e.detail.value * that.data.vipLevelInfo.levelRebate / 100)
      console.log(that.data.vipLevelInfo.levelRebate)
      setTimeout(function() {
        that.setData({
          total: e.detail.value,
          amt: (e.detail.value * (that.data.vipLevelInfo.levelRebate / 100)).toFixed(2),
          couponAmt: (e.detail.value - e.detail.value * (that.data.vipLevelInfo.levelRebate / 100)).toFixed(2)
        })
      }, 500)
    } "" != e.detail.value ? this.setData({
      kong: !1,
    }) : this.setData({
      kong: !0,
    });
  },
  formSubmit: function(t) {
    console.log(t)
    console.log(t.detail.value.radiogroup)
    if (t.detail.value.radiogroup) {
      if (t.detail.value.radiogroup == "wxzf") {
        that = this
        // 提交订单
        that.pay(1, t.detail.value.radiogroup)
      }
      if (t.detail.value.radiogroup == "yezf") {
        var z = wx.getStorageSync('bizUserInfo').balance,
          D = Number(this.data.amt);
        if (console.log(z, D), z < D) return void wx.showToast({
          title: "余额不足支付",
          icon: "loading"
        });
        that.pay(4, t.detail.value.radiogroup)
      }
    } else {
      wx.showToast({
        title: '请先选择支付方式！',
        icon: 'none'
      })
    }
  },
  // 优惠券弹窗
  qlq: function() {
    console.log(this.data), this.setData({
      qlq: !1
    });
  },
  qdzz: function() {
    this.setData({
      qlq: !0
    });
  },
  tabClick: function(t) {
    var e = t.currentTarget.id;
    console.log(e), this.setData({
      activeIndex: t.currentTarget.id
    });
  },
  // 获取优惠券信息
  lqyhq: function(t, e) {
    var that = this;
    app.getData({
      url: app.api.getWdkq(),
      method: 'GET',
      data: {
        status: 1,
        memberId: wx.getStorageSync("bizUserInfo").id,
        companyId: app.globalData.companyId
      },
      ok: res => {
        console.log(res)
        if (res.statusCode == 200 && res.errMsg == 'request:ok') {
          if (res.data.code == 1) {
            console.log("优惠券信息", res.data.data);
            for (var cd = res.data.data, djq = [], zkq = [], i = 0; i < cd.length; i++) {
              cd[i].couponType == 1 ? djq.push(cd[i]) : zkq.push(cd[i])
            }
            console.log(djq, zkq)
            that.setData({
              djq: djq,
              zkq: zkq
            })
          }
        }
      }
    })
  },
  radioChange: function(t) {
    this.setData({
      radioChange: t.detail.value
    }), console.log("radio发生change事件，携带value值为：", t.detail.value);
  },
  xzq: function(t) {
    console.log(t)
    if (console.log(t.currentTarget.dataset, this.data.total), Number(t.currentTarget.dataset.coupon) > this.data.total) return wx.showModal({
      title: "提示",
      content: "您的消费金额不满足此优惠券条件"
    }), !1;
    this.setData({
      conponId: t.currentTarget.dataset.rdid,
      amt: (that.data.total * (that.data.vipLevelInfo.levelRebate / 100)).toFixed(2)
    })
    if ("代金券" == t.currentTarget.dataset.type) {
      this.setData({
        kdje: t.currentTarget.dataset.full
      })
    };
    if ("折扣券" == t.currentTarget.dataset.type) {
      console.log((0.1 * Number(t.currentTarget.dataset.full)).toFixed(2))
      console.log(((0.1 * Number(t.currentTarget.dataset.full)) * this.data.amt).toFixed(2))
      this.setData({
        kdje: ((1 - 0.1 * Number(t.currentTarget.dataset.full)) * Number(this.data.amt)).toFixed(2)

      })
    };
    this.setData({
      couponId: t.currentTarget.dataset.rdid,
      amt: (that.data.amt - that.data.kdje).toFixed(2)
    });
    that.qdzz()
  },
  radioChange: function(t) {
    console.log(t)
    console.log("radio1发生change事件，携带value值为：", t.detail.value), "wxzf" == t.detail.value && this.setData({
      zffs: 1,
      zfwz: "微信支付",
      btntype: "btn_ok1",
    }), "yezf" == t.detail.value && this.setData({
      zffs: 2,
      zfwz: "余额支付",
      btntype: "btn_ok2",
    });
  },
  yczz: function() {
    this.setData({
      showModal: !1
    });
  },
  showModel: function() {
    if (!this.data.total) {
      wx.showToast({
        title: '请先输入消费金额！',
        icon: 'none'
      })
    } else {
      this.setData({
        showModal: !0
      })
    }
  },
  pay: function(payType, type) {
    //添加订单
    app.getData({
      url: app.api.getMaidan(),
      method: "POST",
      data: {
        companyId: app.globalData.shopData.companyId,
        price: that.data.total,
        amt: that.data.amt,
        payType: payType,
        memberId: wx.getStorageSync('bizUserInfo').id,
        couponId: that.data.couponId,
        couponAmt: that.data.kdje
      },
      ok: function(res) {
        console.log(res)
        //  添加订单成功之后去支付
        if (type == "wxzf") {
          that.wxPay(res.data.data.id)
        }
        if (type == "yezf") {
          wx.navigateTo({
            url: '../../../../../../mine/pages/wdzd/wdzd?payId=1',
          })
          that.getBizUserInfo()
        }
        that.setData({
          showModal: !1,
          inputValue: '',
        })
      }
    })
  },
  getBizUserInfo: function() {
    var that = this
    app.getData({
      url: app.api.getBizUserInfo(),
      data: {
        openid: wx.getStorageSync('openId')
      },
      ok: function(res) {
        console.log(res)
        if (res.statusCode == 200 && res.errMsg == 'request:ok') {
          if (res.data.code == 1) {
            wx.setStorageSync('bizUserInfo', res.data.data)
          }
        }
      }
    })
  },
  wxPay: function(order) {
    var openId = wx.getStorageSync("openId")
    app.getData({
      url: app.api.toPay(order, openId, 4), //仅为示例，并非真实的接口地址
      method: 'GET',
      ok: function(e) {
        console.log(e)
        wx.requestPayment({
          timeStamp: JSON.stringify((JSON.parse(e.data.params)).timeStamp),
          nonceStr: (JSON.parse(e.data.params)).nonceStr,
          package: (JSON.parse(e.data.params)).package,
          signType: 'MD5',
          paySign: (JSON.parse(e.data.params)).paySign,
          success: function(res) {
            // wx.showToast({
            //   title: "支付成功",
            //   duration: 800
            // }),
            wx.navigateTo({
              url: '../../../../../../mine/pages/wdzd/wdzd'
            })
          },
          fail: function(res) {
            console.log(e)
            wx.showToast({
              title: '支付失败',
              duration: 2000
            })
          },
          complete: function(res) {}
        })
      }
    })
  }
})